﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Tool.UserControls;

namespace ERP_RORZE_TOOL.Tool.Forms
{
	public partial class frm_ToolEdit : DevExpress.XtraEditors.XtraForm
	{
		public string ToolId;
		public string ToolName;
		public string MakerToolName;
        public decimal Price;
		public int SafeQty;
		public string FrequencyId;
		public string ToolGroupId;
		public string MakerId;
		private ucTool parent;

		public frm_ToolEdit(ucTool fTool)
		{
            InitializeComponent();
            btSave.Image = ERP_RORZE_TOOL.Properties.Resources.accept;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
			parent = fTool;
			this.CancelButton = this.btCancel;
		}

		private void btSave_Click(object sender, EventArgs e)
		{
			ToolHelper helper = new ToolHelper(new Tools());
			if(GetData(helper.ValueObject))
			{
				

				if(helper.Update() > 0)
				{

                    if (!Utilities.MessageConfirmationYesNo(ToolConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
				else
				{
					
					Utilities.MessageErrorOk(ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frm_ToolEdit_Load(object sender, EventArgs e)
		{
			BindData();
			BindDataToFrequency();
			BindDataToToolGroup();
            BindDataMaker();
		}

		private void txtToolID_KeyPress(object sender, KeyPressEventArgs e)
		{

		}

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
		//Private methods
		#region Private methods

		private bool CheckDuplicate(string id)
		{
			ToolHelper helper = new ToolHelper(new Tools());
			helper.ValueObject.ToolID = id;
			DataTable dt = helper.GetOne();
			if(dt.Rows.Count > 0)
				return true;
			return false;

		}

		private bool GetData(Tools o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtToolID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtToolName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.ToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtMakerToolname.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.MakerToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtPrice.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtSafeQty.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Tool.SafeQtyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

            if (Utilities.IsNullOrEmpty(cmbFrequency.SelectedValue) || cmbFrequency.SelectedItem == cmbFrequency.Items[0])
			{
				MessageBox.Show(ToolConstants.Message.Tool.FrequencyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

            if (Utilities.IsNullOrEmpty(cmbToolGroup.SelectedValue) || cmbToolGroup.SelectedItem == cmbToolGroup.Items[0])
			{
				MessageBox.Show(ToolConstants.Message.Tool.ToolGroupCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			if(string.Compare(txtToolID.Text.Trim(), ToolId) != 0)
			{
				if (CheckDuplicate(txtToolID.Text.Trim()))
				{
					MessageBox.Show(ToolConstants.Message.Tool.ToolHasExisted, ToolConstants.Form.MessageBox.Error,
					                MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}
			}

			#endregion

			o.TempId = txtToolID.Text;
			o.ToolID = ToolId;
			o.ToolName = txtToolName.Text;
			o.MakerToolName = txtMakerToolname.Text;
            o.Price = decimal.Parse(txtPrice.Text);
			o.SafeQty = int.Parse(txtSafeQty.Text);
			o.FrequencyID = cmbFrequency.SelectedValue.ToString();
			o.ToolGroupID = cmbToolGroup.SelectedValue.ToString();
            o.MakerID = cmbMaker.SelectedValue.ToString();

			return true;
		}

		private void BindData()
		{
			txtToolID.Text = ToolId;
			txtToolName.Text = ToolName;
			txtMakerToolname.Text = MakerToolName;
			txtPrice.Text = Price.ToString();
			txtSafeQty.Text = SafeQty.ToString();
		}

		private void BindDataToFrequency()
		{
			FrequencyHelper helper = new FrequencyHelper(new Frequency());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Frequency.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Frequency.FieldName.FrequencyID] = null;
			dt.Rows.InsertAt(row, 0);

            cmbFrequency.DataSource = dt;
            cmbFrequency.DisplayMember = ToolConstants.Entities.Frequency.FieldName.FullName;
            cmbFrequency.ValueMember = ToolConstants.Entities.Frequency.FieldName.FrequencyID;

			cmbFrequency.SelectedValue = FrequencyId;
			if(cmbFrequency.SelectedItem == null)
			{
				cmbFrequency.SelectedItem = cmbFrequency.Items[0];
			}
		}

		private void BindDataToToolGroup()
		{
			ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

            row[ToolConstants.Entities.ToolGroup.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID] = null;
			dt.Rows.InsertAt(row, 0);

            cmbToolGroup.DataSource = dt;
            cmbToolGroup.DisplayMember = ToolConstants.Entities.ToolGroup.FieldName.FullName;
            cmbToolGroup.ValueMember = ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID;

			cmbToolGroup.SelectedValue = ToolGroupId;
			if(cmbToolGroup.SelectedItem == null)
			{
				cmbToolGroup.SelectedItem = cmbToolGroup.Items[0];
			}
		}

        private void BindDataMaker()
        {
            MakerHelper helper = new MakerHelper(new Maker());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Maker.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Maker.FieldName.MakerID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMaker.DataSource = dt;
            cmbMaker.DisplayMember = ToolConstants.Entities.Maker.FieldName.FullName;
            cmbMaker.ValueMember = ToolConstants.Entities.Maker.FieldName.MakerID;

            cmbMaker.SelectedValue = MakerId;
            if (cmbMaker.SelectedItem == null)
            {
                cmbMaker.SelectedItem = cmbMaker.Items[0];
            }
        }
		#endregion



	}
}